package org.example.car.system.controller;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import org.example.car.system.domain.SysMenu;
import org.example.car.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/menu")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;


    @RequestMapping("/menuList")
    public List<Tree<String>> menuList(@RequestBody SysMenu entity){
        System.out.println(sysMenuService);
        List<SysMenu> list = sysMenuService.listQuery(entity);
        System.out.println(list);
        //配置
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();

        treeNodeConfig.setIdKey("menuId");
        treeNodeConfig.setChildrenKey("children");
        treeNodeConfig.setNameKey("menuName");
        treeNodeConfig.setParentIdKey("parentId");
// 最大递归深度
//转换器 (含义:找出父节点为字符串零的所有子节点, 并递归查找对应的子节点, 深度最多为 3)
        List<Tree<String>> treeNodes = TreeUtil.<SysMenu, String>build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getMenuId()+"");
                    tree.setParentId(treeNode.getParentId()+"");
                    tree.setName(treeNode.getMenuName());
                    tree.putExtra("path",treeNode.getPath());
                    tree.putExtra("component",treeNode.getComponent());
                    tree.putExtra("status",treeNode.getStatus());
                    tree.putExtra("icon",treeNode.getIcon());
                    tree.putExtra("perms",treeNode.getPerms());
                    tree.putExtra("orderNum",treeNode.getOrderNum());
                    tree.putExtra("createTime",treeNode.getCreateTime());
                });
        return treeNodes;
    }


    @RequestMapping("addorupdateForm")
    public Map<String,Object> addorupdateForm (@RequestBody SysMenu entity) throws Exception{
        HashMap<String,Object> map = new HashMap();
        try {
            if (entity.getMenuId()!=null){
                entity.setCreateTime(new Date());
                sysMenuService.updateForm(entity);
                map.put("code",200);
                map.put("data",entity);
                map.put("msg","修改成功!");
            }else{
                sysMenuService.insertbyquery(entity);
                map.put("code","200");
                map.put("data",entity);
                map.put("msg","添加成功!");
            }
        }catch (Exception exception){
            exception.printStackTrace();
            map.put("code",201);
            map.put("msg","添加失败!");
        }
        return map;
    }


    @RequestMapping("deleteForm")
    public Map<String,Object> deleteForm(Integer menuId){
        HashMap<String, Object> map = new HashMap<>();
        try {
            sysMenuService.deleteByQuery(menuId);
            map.put("code",200);
            map.put("msg","删除成功!");
        }catch (Exception exception){
            exception.printStackTrace();
            map.put("code",505);
            map.put("msg","删除失败!");
        }
        return map;
    }


//    @RequestMapping("getParentId")
//    public Map<Object,Object> getParentId(@RequestParam Integer parentId){
//        HashMap<Object, Object> map = new HashMap<>();
//        List<MenuTree> list = menuTreeService.menuTreeList(parentId);
//        map.put("list",list);
//        return map;
//    }

    @RequestMapping("getParentPath")
    public Map<Object,Object> getParentIdPath(@RequestParam Integer parentId){
        System.out.println("-------------");
        HashMap<Object, Object> map = new HashMap<>();
        List<Integer> path = new ArrayList<>();
        path.add(0,parentId);
        Integer a = parentId;
        while (a!=null) {
            System.out.println(a);
            SysMenu sysMenu=sysMenuService.sentimentality(parentId);
            a= sysMenu.getParentId();
            if (a!=null){
                path.add(0,a);
            }
        }
        System.out.println(path);
        map.put("path",path);
        return map;
    }

    @RequestMapping("list")
    public List<Tree<String>> list(){
        return sysMenuService.selecaddtee();
    }


}
